/**
 * @file s51_spi.c
 * @author your name (you@domain.com)
 * @brief 
 * @version 1.0
 * @date 2020-03-11
 * 
 * @copyright Copyright (c) 2020
 * 
 */

#include "../Include/s51_io_spi.h"

void Simple51_SPI_Init(SPI_HandleTypeDef *hspi)
{
    if (hspi->Instance == HSPI1)
    {
    }
}

/**
 * @brief SPI4-wire communication function, 
 *        including all 4-wire 8-bit communication modes, with high order first.
 * 
 * @param hspi SPI handle
 * @param _data Data to be sent
 * @return uint8_t Returns data read as
 */
uint8_t Simple51_SPI_TransmitReceiveByte(SPI_HandleTypeDef *hspi, uint8_t _data)
{
    uint8_t i;
    uint8_t __getdata = 0x00;
    if (hspi->Instance == HSPI1)
    {
        pin_MOSI_1_HIGH, pin_MISO_1_HIGH;
        (hspi->SetCPOL == RESET) ? (pin_SCLK_1_LOW) : (pin_SCLK_1_HIGH);
        for (i = 0; i < 8; i++)
        {
            if (hspi->SetCPHA == SET)
            {
                (hspi->SetCPOL == RESET) ? (pin_SCLK_1_HIGH) : (pin_SCLK_1_LOW);
            }
            (_data & 0x80) ? (pin_MOSI_1_HIGH) : (pin_MOSI_1_LOW), _data = _data << 1;
            __getdata = __getdata << 1, (pin_MISO_1_READ) ? (__getdata |= 1) : (__getdata += 0);
            if (hspi->SetCPHA == RESET)
            {
                (hspi->SetCPOL == RESET) ? (pin_SCLK_1_HIGH) : (pin_SCLK_1_LOW);
            }
            (hspi->SetCPOL == RESET) ? (pin_SCLK_1_LOW) : (pin_SCLK_1_HIGH);
        }
    }
    return __getdata;
}

/**
 * @brief 
 * 
 * @param hspi 
 * @param _txData 
 * @param _size 
 */
void Simple51_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *_txData, uint16_t _size)
{
    if (_size == 0)
    {
        while (*_txData > 0)
        {
            Simple51_SPI_TransmitReceiveByte(hspi, *_txData);
            _txData++;
        }
    }
    else
    {
        for (; _size > 0; _size--)
        {
            Simple51_SPI_TransmitReceiveByte(hspi, *_txData);
            _txData++;
        }
    }
}

/**
 * @brief 
 * 
 * @param hspi 
 * @param _rxData 
 * @param _size 
 */
void Simple51_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *_rxData, uint16_t _size)
{
    if (_size == 0)
    {
        while (*_rxData > 0)
        {
            *_rxData = Simple51_SPI_TransmitReceiveByte(hspi, 0);
            _rxData++;
        }
    }
    else
    {
        for (; _size > 0; _size--)
        {
            *_rxData = Simple51_SPI_TransmitReceiveByte(hspi, 0);
            _rxData++;
        }
    }
}

/**
 * @brief 
 * 
 * @param hspi 
 * @param _txData 
 * @param _rxData 
 * @param _size 
 */
void Simple51_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *_txData, uint8_t *_rxData, uint16_t _size)
{
    if (_size == 0)
    {
        while (*_rxData > 0)
        {
            *_rxData = Simple51_SPI_TransmitReceiveByte(hspi, *_txData);
            _rxData++;
            _txData++;
        }
    }
    else
    {
        for (; _size > 0; _size--)
        {
            *_rxData = Simple51_SPI_TransmitReceiveByte(hspi, *_txData);
            _rxData++;
            _txData++;
        }
    }
}